<template>
  <div class="articleManage">
    <div class="iptRow">
      <el-input v-model="articleTitle" placeholder="资讯标题"></el-input>
      <el-radio v-model="value" :label="0">全部</el-radio>
      <el-radio v-model="value" :label="-1">轮播图</el-radio>
      <el-radio v-model="value" :label="1">医院资讯</el-radio>
      <el-button icon="el-icon-search" circle @click="getArticleList"></el-button>
    </div>
    <div class="btnRow">
      <el-button type="primary" @click="add">新增</el-button>
    </div>
    <div class="record">
      <el-table :data="tableList" style="width: 100%">
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-link type="primary" @click="edit(scope.row)">编辑</el-link>
            <el-link type="success" @click="imgDetail(scope.row)">查看图片</el-link>
            <el-link type="danger" @click="del(scope.row)">删除</el-link>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="200">
          <template slot-scope="scope">
            <span v-if="scope.row.state == 1" class="active">启用中</span>
            <span v-if="scope.row.state == -1" class="inActive">停用中</span>
          </template>
        </el-table-column>
        <el-table-column prop="articleId" label="咨询id" width="100"></el-table-column>
        <el-table-column prop="articleTitle" label="属性" width="200">
          <template slot-scope="scope">
            <span>{{ scope.row.value == 1 ? "医院资讯" : "轮播图" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="articleTitle" label="资讯标题" width="300" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="articleContent" label="资讯内容" :show-overflow-tooltip="true"></el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        align="right"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next"
        :total="totalNumber"
        style="margin-top: 58px; margin-bottom: 40px"
      ></el-pagination>
    </div>
    <el-dialog
      :title="diaTitle"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <div class="bbb">
        <el-upload
          class="avatar-uploader"
          action="/api/CjwAdmin/UploadPictures"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>

      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="标题">
          <el-input v-model="form.articleTitle"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="form.articleContent" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="属性">
          <el-radio v-model="form.value" :label="1">医院咨询</el-radio>
          <el-radio v-model="form.value" :label="-1">轮播图</el-radio>
        </el-form-item>
        <el-form-item label="属性">
          <el-radio v-model="form.state" :label="1">启用</el-radio>
          <el-radio v-model="form.state" :label="-1">不启用</el-radio>
        </el-form-item>
        <!-- <el-form-item label="身份证号码">
          <el-input v-model="form.numberId"></el-input>
        </el-form-item>
        <el-form-item label="出生日期">
          <el-date-picker v-model="form.birth" type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="form.sex" label="0">女</el-radio>
          <el-radio v-model="form.sex" label="1">男</el-radio>
        </el-form-item>
        <el-form-item label="职位">
          <el-select v-model="form.position" placeholder="请选择">
            <el-option
              v-for="item in positionList"
              :key="item.positionId"
              :label="item.positionName"
              :value="item.positionName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="个人权限">
          <el-switch
            v-model="form.state"
            active-text="权限正常"
            inactive-text="禁用权限"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>-->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="formConfirm">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="查看图片"
      :visible.sync="dialogVisibleImg"
      width="30%"
      :before-close="handleCloseImg"
    >
      <div class="head">
        <img :src="imageUrl" class="iii" />
      </div>
      <span slot="footer" class="dialog-footer">
        <!-- <el-button @click="dialogVisibleImg = false">取 消</el-button> -->
        <el-button type="primary" @click="dialogVisibleImg = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
   

<script>
export default {
  name: "articleManage",
  data() {
    return {
      value: 0,
      imageUrl: "",
      articleTitle: "",
      articleId: 0,
      dialogVisible: false,
      dialogVisibleImg: false,
      tableList: [],
      pageIndex: 1,
      pageSize: 10,
      totalNumber: 0,
      diaTitle: "",
      form: {
        articleTitle: "",
        articleContent: "",
        value: "",
        state: ""
      }
    };
  },
  created() {
    this.getArticleList();
  },
  methods: {
    getArticleList() {
      let params = {
        articleTitle: this.articleTitle,
        state: 0,
        value: this.value,
        pageIndex: this.pageIndex,
        pageSize: this.pageSize
      };
      this.$axios
        .get("/api/CjwArticleList/GetArticleList", { params })
        .then(res => {
          console.log(res);
          this.tableList = res.data.articleLists;
          this.totalNumber = res.data.total;
        });
    },
    formInit() {
      this.form = {
        articleTitle: "",
        articleContent: "",
        value: 1,
        state: 1
      };
    },
    add() {
      this.formInit();
      this.imageUrl = "";
      this.diaTitle = "新增";
      this.dialogVisible = true;
    },
    edit(item) {
      this.diaTitle = "编辑";
      console.log(item);
      this.dialogVisible = true;
      this.form = {
        articleTitle: item.articleTitle,
        articleContent: item.articleContent,
        value: item.value,
        state: item.state
      };
      this.imageUrl = item.img;
      this.articleId = item.articleId;
    },
    imgDetail(item) {
      this.dialogVisibleImg = true;
      this.imageUrl = item.img;
    },
    del(item) {
      let title = "";
      if (item.value == -1) {
        title = "此操作将会选中的轮播图,是否确定?";
      } else {
        title = "此操作将会选中的医院资讯,是否确定?";
      }
      this.$confirm(title, "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios
            .get("/api/CjwArticleList/DelArticle?articleId=" + item.articleId)
            .then(res => {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.getArticleList();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleClose() {
      this.dialogVisible = false;
    },
    handleCloseImg() {
      this.dialogVisibleImg = false;
    },
    formConfirm() {
      if (this.diaTitle == "新增") {
        let params = {
          articleTitle: this.form.articleTitle,
          articleContent: this.form.articleContent,
          img: this.imageUrl,
          state: this.form.state,
          value: this.form.value
        };
        console.log(params);
        this.$axios.post("/api/CjwArticleList/AddArticle", params).then(res => {
          this.$message({
            type: "success",
            message: "新增成功!"
          });
          this.getArticleList();
          this.dialogVisible = false;
        });
      } else {
        let params = {
          articleId: this.articleId,
          articleTitle: this.form.articleTitle,
          articleContent: this.form.articleContent,
          img: this.imageUrl,
          state: this.form.state,
          value: this.form.value
        };
        this.$axios
          .post("/api/CjwArticleList/EditArticle", params)
          .then(res => {
            this.$message({
              type: "success",
              message: "编辑成功!"
            });
            this.getArticleList();
            this.dialogVisible = false;
          });
      }
    },
    handleAvatarSuccess(res, file) {
      // console.log(res);
      // console.log(file);
      this.imageUrl = res;
      this.url = res;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getArticleList();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getArticleList();
    }
  }
};
</script>

<style scoped>
.iptRow {
  margin: 10px;
}
.iptRow .el-input {
  width: 280px;
}
.btnRow {
  margin: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.bbb {
  display: flex;
  justify-content: center;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.active {
  color: black;
  background-color: #76ee00;
  border-radius: 8px;
}
.inActive {
  color: black;
  background-color: #8a8a8a;
  border-radius: 8px;
}
.head {
  width: 100%;
  height: 100%;
}

.head .iii {
  height: 100%;
  width: 100%;
}
</style>